import numpy as np
from speech_recognition import XfyunSpeechRecognizer
from new_chat import SparkAPI
from extract_json import extract_json
from arm import ArmController
from detect import Detect
from frame_manager import FrameManager
from transform_matrix import matrix,matrix_laolaobei
import time
from voice import Voice
OBG_HEIGHT={"wanzaimian":-4.4356536865234375,"shengyenatie":-25.962739944458008,"qinzuishao":-61.44023513793945,"laolaobei":15.969478607177734,"xingqiubei":-39.03107833862305,"nailaoxiaowanzi":-50.7457275390625,"dougan":-55.27495193481445,"shuimitaoguozhi":-33.494903564453125,"xizhilang":-9.559593200683594}
APP_ID = "72b9a0ad"
API_KEY = "59958ac9ffdaed40d02509f4bb593774"
API_SECRET = "NmMyYTRlZjFhMmZkN2FkZjg0ODQ5NDVl"
DOMAIN = "x1"  # 控制请求的模型版本
# 服务地址
SPARK_URL = "wss://spark-api.xf-yun.com/v1/x1"
# 创建API客户端
client = SparkAPI(APP_ID, API_KEY, API_SECRET, SPARK_URL, DOMAIN)
# 创建语音播放实例
voice = Voice()
# 创建机械臂实例
arm = ArmController()
arm.return_init()
print("==================机械臂初始化完毕===============")
voice.play(path=r"E:\Innovative Practice\MyProject\voice\init_arm_done.mp3")
frame_manager = FrameManager()
frame_manager.start_camera(0)
print("==================摄像机初始化完毕===============")
voice.play(path=r"E:\Innovative Practice\MyProject\voice\init_camera_done.mp3")
detector = Detect(frame_manager, conf_threshold=0.5)
voice.play(path="E:/Innovative Practice/MyProject/voice/welcome.mp3")

# 定义结果处理函数（回调函数）
def handle_recognition_result(result, recognizer):
    """处理识别结果的回调函数"""
    try:
        print(f"语音识别收到结果: {result}")
        res = client.chat(result)  # 将识别结果传递给星火大模型进行处理
        res = extract_json(res)
        print(f"返回指令为：{res}")
        start_pose = None
        target_pose = None
        if res:
            for item in res:
                if (item.get("object") and item.get("target_pos")):
                    detect_res = detector.detect_and_process_multiple_frames(num_frames=3)
                    print(f"识别结果为：{detect_res}")
                    if detect_res:
                        for i in detect_res:
                            if i['class'] == item.get("object"):
                                print("识别到目标物体")
                                pix_pose = i['coordinates']
                                print(f'初始物体像素位置{pix_pose}')
                                if item.get("object") == "laolaobei":
                                    start_pose = list(np.dot(matrix_laolaobei, np.array([pix_pose[0], pix_pose[1], 1]).T))
                                else:
                                    start_pose = list(np.dot(matrix, np.array([pix_pose[0], pix_pose[1], 1]).T))
                                start_pose[2] = OBG_HEIGHT[item.get("object")]
                                # start_pose[2] = 80
                                start_pose.append(-30)
                                print(f"初始物体世界坐标位置{start_pose}")

                            if i['class'] == item.get("target_pos"):
                                print("识别到目标位置")
                                pix_pose = i['coordinates']
                                print(f"目标像素位置{pix_pose}")
                                target_pose = list(np.dot(matrix, np.array([pix_pose[0], pix_pose[1], 1]).T))
                                target_pose[2] = 45
                                target_pose.append(-30)
                                print(f"目标世界坐标位置{target_pose}")
                        #放在手上
                        if start_pose and target_pose:
                            print("开始抓取")
                            arm.grasp_place(start_pose, target_pose)
                            voice.play(name=item.get("object"))
                            voice.play(path=r"E:\Innovative Practice\MyProject\voice\pay_for5.mp3")
                            arm.return_init()
                        #放在指定地点
                        if start_pose and not target_pose:
                            print("开始抓取")
                            arm.grasp_place(start_pose, [178.05740356445312, 209.456298828125, 25.23737335205078, -30])
                            voice.play(name=item.get("object"))
                            voice.play(path=r"E:\Innovative Practice\MyProject\voice\pay_for5.mp3")
                            arm.return_init()
        else:
            print("无法提取JSON")
    finally:
        # 回调函数执行完毕，设置"完成"标志位
        with recognizer.callback_lock:
            recognizer.callback_in_progress = False
            recognizer.callback_completed = True


def main():
    # 创建语音识别器实例
    recognizer = XfyunSpeechRecognizer(APP_ID, API_KEY, API_SECRET)

    # 设置回调函数（关键步骤）
    recognizer.set_callback(handle_recognition_result)

    # 启动持续识别（手动模式）
    # use_manual_mode = True
    try:
        recognizer.start_listening()
        # 保持主程序运行
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        recognizer.stop_recognition()
        print("已手动停止识别")


if __name__ == "__main__":
    main()
